package com.ruoyi.management.service.impl;


import com.ruoyi.management.dao.mapper.TemplateMapper;
import com.ruoyi.management.pojo.po.UserPO;
import com.ruoyi.management.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TemplateImpl implements TemplateService {

    @Autowired
    private TemplateMapper templateMapper;

    @Override
    public boolean updateOpenidByUnionId(String unionId, String openid) {
        // 1. 根据unionId查询数据库中的用户
        List<UserPO> userPO=templateMapper.selectUserByUnionId(unionId);
        if (userPO == null || userPO.isEmpty()){
            log.warn("用户[UnionID: {}]不存在", unionId);
            return false;
        }
        for (UserPO user : userPO){
            if (openid.equals(user.getOpenIdGzh())){
                log.info("用户[{}]的openid未变化，无需更新", user.getXm());
                return true;
            }
            // 3. 执行更新操作
            int row=templateMapper.updateOpenidById(user.getYhID(), openid);
            if (row > 0){
                log.info("用户[{}]的openid更新成功", user.getXm());
                return true;
            }else{
                log.error("用户[{}]的openid更新失败", user.getXm());
                return false;
            }
        }
        return false;
    }
}
